package Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo16 {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Callable<Integer> callable=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum=0;
                for (int i = 0; i <=1000 ; i++) {
                    sum+=i;
                }
                return sum;
            }
        };
        FutureTask<Integer> futureTask=new FutureTask<>(callable);
        Thread t1=new Thread(futureTask); //无法直接传callable作为参数 ，此处需要搭配futureTask
        t1.start();
        t1.join();
        //get方法拿到call的返回值
        //如果call没有执行完，get就会阻塞等待
        System.out.println(futureTask.get());
    }
}
